//
//  HLEditSignatureViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLEditSignatureViewController.h"
#define defaultSignature @"TA太懒了还没有补充个性签名~"

@interface HLEditSignatureViewController ()<QMUITextViewDelegate>

@property (nonatomic, strong) QMUIButton *saveButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) QMUITextView *textView;
@property (nonatomic, strong) UILabel *textCountLabel;
@property (nonatomic, strong) NSString *signature;
@property (nonatomic, copy) HLCommonCallbackStringBlock callback;

@end

@implementation HLEditSignatureViewController

- (instancetype)initWithSignature:(NSString *)signature callback:(HLCommonCallbackStringBlock)callback
{
    self = [super init];
    if (self){
        self.signature = signature;
        self.callback = callback;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorWhite;
    if(self.signature.length > 0 && ![self.signature isEqualToString:defaultSignature]){
        self.textView.text = self.signature;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:({
        QMUIButton *saveButton = [[QMUIButton alloc] init];
        [saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [saveButton.titleLabel setFont:UIFontMediumMake(12)];
        [saveButton setBackgroundColor:HLMainColor];
        [saveButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
        [saveButton setContentEdgeInsets:UIEdgeInsetsMake(6, 12, 6, 12)];
        saveButton.cornerRadius = 14;
        [saveButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
        self.saveButton = saveButton;
        saveButton;
    })];
    
    self.navigationItem.rightBarButtonItem = saveItem;
}

- (void)initSubviews
{
    [super initSubviews];
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.font = UIFontMediumMake(15);
    _titleLabel.textColor = HLTextColor1;
    _titleLabel.text = @"编辑内容";
    [self.view addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(NavigationContentTopConstant + 12);
    }];
    
    self.textView = [[QMUITextView alloc] init];
    _textView.placeholder = @"写点什么让别人更了解你…";
    _textView.font = UIFontMake(14);
    _textView.textColor = HLTextColor1;
    _textView.contentInset = UIEdgeInsetsMake(12, 12, 12, 12);
    _textView.backgroundColor = HLBackgroundColor;
    _textView.maximumTextLength = 100;
    _textView.delegate = self;
    [self.view addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(_titleLabel.mas_bottom).offset(12);
        make.height.mas_equalTo(170);
    }];
    
    self.textCountLabel = [[UILabel alloc] init];
    _textCountLabel.font = UIFontMake(12);
    _textCountLabel.textColor = HLTextColor2;
    _textCountLabel.text = @"0/100";
    [self.view addSubview:_textCountLabel];
    [_textCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(_textView).offset(-12);
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDarkContent;
}

- (UIImage *)qmui_navigationBarBackgroundImage
{
    return [UIImage qmui_imageWithColor:UIColorWhite];
}

- (nullable UIColor *)qmui_navigationBarTintColor
{
    return UIColorBlack;
}

- (void)saveAction
{
//    if (self.textView.text.length < 6){
//        [QMUITips showWithText:@"昵称必须大于5个字符".wf_localized];
//        return;
//    }
    
    if (self.textView.text.length > 100){
        [QMUITips showWithText:@"昵称必须小于100个字符"];
        return;
    }
    
    
    if (self.callback){
        self.callback(self.textView.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma QMUITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView
{
    self.textCountLabel.text = [NSString stringWithFormat:@"%ld/100", textView.text.length];
}

- (BOOL)textView:(QMUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text originalValue:(BOOL)originalValue
{
//    if (textView.text.length > 5){
//        [self.saveButton setBackgroundColor:HLMainColor];
//        [self.saveButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
//    } else {
//        [self.saveButton setTitleColor:HLTextColor3 forState:UIControlStateNormal];
//        [self.saveButton setBackgroundColor:HLBackgroundColor];
//    }
    
    return YES;
}

@end
